const app = getApp();
import {$await} from '../../utils/util';
const CONFIG = require('../../config.js');
import {getTiming} from '../../api/timing/index';
import {getUserInfo,commonAPI} from '../../api/common/index';

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		channelId:wx.getStorageSync('localChannelId'),
		area:"石家庄",
		chat:'true',
		imgUrl: 'https://images.weserv.nl/?url=' + CONFIG.imgUrlImg,
		hotObj: '',
		navBarObj: {
			left:26,
			bgColor: '#F9F9F9',
			showBackBtn: true,
			title: "广播"
		},
		liveList: [{
				"id": 0,
				"mark": '/images/zcr-icon.png',
				"src": '/images/common/discuss.png',
				"name": '交通一点通',
				"title": '五一小长假，打卡网红地',
			},
		],
		brandObj: {
			name: '上汽大众',
			title: '广告',
			src: '/images/indexSelect.png',
			url:'kjhkjknk'
		},
		interval: 2000,
		duration: 500,
		currentIndex: 0,
		metudIcon:'../../images/metod-icon.png',
		openIcon:'../../images/bo-icon.png',
		muted: true,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLaunch(){
		getApp().globalData.tabBarType = 'local';
	},
	onLoad: function(options) {
		let {channelId,pid} = options;
		if(options.channelId){
			this.setData({
				channelId // 频率id
			})
			wx.setStorageSync('localChannelId', channelId)
			
		}else{
			this.setData({
				channelId:wx.getStorageSync('localChannelId') //  频 率 id
			})  
		}
		wx.setStorageSync('pid', pid)
		getTiming()
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function(res) {
		
		let _this = this;
		_this.ctx = wx.createLivePlayerContext('player');
		_this.ctx.play({
			success: res => {console.log('播放成功')},
			fail: res => {console.log('播放失败，失败原因' + res.errMsg)}
		})
		getUserInfo();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		console.log(wx.getStorageSync('localChannelId'))
		this.init()
	},
	async liveData(id){
		let data={
			type: 1,
			channel_id: id,
		}
		let [err,res] = await $await(commonAPI.liveData(data));
		let temp = "navBarObj.title";
		if(res){
			this.setData({
				live_data: res.live_data.play,
				live_banner:res.live_data.banner,
				[temp]:res.live_data.play.channel_name
			})
			
		}
	},
	async localData(cId){
		let data={id:cId}
		let [err,res] = await $await(commonAPI.localData(data));
		if(res){
			this.setData({
				obj: res,
				['hotObj.list']: res.top.videos,
			})
			console.log(res.top.videos)
		}
	},
	async localMusic(id){
		let data={channel_id:id}
		let [err,res] = await $await(commonAPI.getMusiclocal(data));
		if(res){
			this.setData({
				musicObj: res,
			})
		}
	},
	async getLive(){
		let data={id:3}
		let [err,res] = await $await(commonAPI.getlive(data));
		if(res){
			this.setData({
				liveList: res.list,
			})
		}
	},
	async getAd(){
		let data = {id:1,area: this.data.area}
		let [err,res] = await $await(commonAPI.getad(data))
		if(res){
			this.setData({
				brandObj: res.list[0],
			})
		}
	},
	init(){

		this.liveData(this.data.channelId);
		this.localData(this.data.channelId);
		this.localMusic(this.data.channelId);
		// this.getAd()
		// this.getLive();
		
		
	},
	
	statechange(e) {
		console.log('live-player code:', e.detail.code)
	},
	error(e) {
		console.error('live-player error:', e.detail.errMsg)
	},
	bannerChange: function(e) {
		let current = e.detail.current;
		console.log(current)
		this.setData({
			currentIndex: current
		})
	},
	submit(e){
		console.log(e.detail.currentTarget.dataset)
	},
	toWeb(e){
		let cont = e.currentTarget.dataset.cont;
		if(cont.jump_type==1){
			wx.navigateTo({
				url: '/pages/webview/webview?webURL=' + cont.obj_url
			})
		}else if(cont.jump_type==2){
			this.checkStatus(cont.obj_id)
		}
		console.log(e.currentTarget.dataset.cont)
	},
	async checkStatus(number) {
		let data = {number}
		let [err, res] = await $await(commonAPI.check(data));
		if (res.status==0 || res.live_number==0) {	
			wx.showToast({
			      title: '暂无直播',
			      icon: 'none',
			      duration: 2000,
			})
		}else{
			let url = "/pages/channelLive/channelLive?number=" + number + ""
			wx.navigateTo({
				url
			})
		}
	},
	mutedTap() {
		this.setData({
			muted: !this.data.muted
		})
		console.log(this.data.muted)
	},
	goLive() {
		if (this.data.live_data.number) {
			this.checkStatus(this.data.live_data.number);			
		}
	},
	hotOpen(e){
		console.log(e.detail);
		if(e.detail){
			this.setData({
				muted:true
			})
		}
	},
	switchStreamDate(e){
		console.log(e.detail)
		let tNumber=this.data.live_data.number;
		let stream = e.detail.content;
		let live_url = 'live_data.live_url';
		let status = 'live_data.status';
		//type:1切视频 type:2切音频 type:3重新调接口
		if(stream.type==1 && stream.number==tNumber){
			this.setData({
				[status]:1,
			})			
		}else if(stream.type==2 && stream.number==tNumber){
			this.setData({
				[status]:2,
			})
		}else{
			this.living(stream.number);
		}	
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function(res) {
		return {
		      title: '听见广播',
		      imageUrl: "",	
		    }
	}
})